package flyshoes.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import flyshoes.mybatis.entity.PageQueryResult;
import flyshoes.mybatis.entity.Product;
import flyshoes.mybatis.entity.Result;
import flyshoes.mybatis.entity.ShoppingCart;
import flyshoes.mybatis.service.serviceImpl.CategoryServiceImpl;
import flyshoes.mybatis.service.serviceImpl.ProductServiceImpl;
import flyshoes.query.ShoesQuery;
import flyshoes.utils.ThreadLocalUtil;
import io.lettuce.core.dynamic.annotation.Param;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class ItemServiceImpl {
    private final ProductServiceImpl productService;
    private final CategoryServiceImpl categoryService;
    private final RedisTemplate redisTemplate;

    @Transactional
    public Result getAllCategory() { //获得所有的分类
        redisTemplate.opsForValue().set("allCategory:", categoryService.lambdaQuery().list(), 168, TimeUnit.HOURS);
        return Result.success(categoryService.lambdaQuery().list());
    }

    @Transactional
    public Result getShoesBycateId(Integer cateId) { //通过分类id查商品
        String redisKey = "getShoesByCateId:" + cateId;
        redisTemplate.opsForValue().set(redisKey, productService.lambdaQuery().eq(Product::getCategoryId, cateId).list(), 168, TimeUnit.HOURS);
        return Result.success(productService.lambdaQuery().eq(Product::getCategoryId, cateId).list());
    }

    @Transactional
    public Result getOneShoes(Integer productId) {

        return Result.success(productService.lambdaQuery().eq(Product::getId, productId).one());
    }

    @Transactional
    public Result getShoesByName(String productName) {
        return Result.success(productService.lambdaQuery().eq(Product::getProductName, productName).one());
    }

    @Transactional
    public Result queryShoes(ShoesQuery shoesQuery) { //根据条件查询和筛选商品
        String condition = shoesQuery.getCondition();
        Integer minPrice = shoesQuery.getMinMoney();
        Integer maxPrice = shoesQuery.getMaxMoney();
        Page<Product> page = shoesQuery.toMpPageDefaultSortByPrice();
        List list = productService.lambdaQuery()
                .like(condition != null, Product::getProductName, condition)
                .or()
                .like(condition != null, Product::getBrand, condition)
                .or()
                .like(condition != null, Product::getProductDescription, condition)
                .ge(minPrice != null, Product::getPrice, minPrice)
                .le(maxPrice != null, Product::getPrice, maxPrice)
                .list();

        if (list.isEmpty()) {
//            PageQueryResult pageQueryResult = new PageQueryResult<>();
//            pageQueryResult.setMsg("暂无商品");
            return Result.fail(400,"暂无商品");
        }
//        PageQueryResult queryResult = new PageQueryResult<>();
//        queryResult.setTotal(p.getTotal());
//        queryResult.setPage(p.getPages());
//        queryResult.setList(p.getRecords());
//        queryResult.setMsg("成功");
        return Result.success(list);
    }

}
